package com.pencilisland.pay.mgt.console.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pencilisland.pay.mgt.console.service.AuthService;

/**
 * 授权处理器
 * 
 * @author Breeze
 *
 */
@Controller
@RequestMapping(value = "/console/auth")
public class AuthController {

	@Autowired
	AuthService authService;

	/**
	 * 进入授权页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "")
	public String toAuth() {
		return "console/auth";
	}

	/**
	 * 加载数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/load")
	@ResponseBody
	public Map<String, Object> load(@RequestParam(value = "pobjId") Long pobjId,
			@RequestParam(value = "pobjType") String pobjType, @RequestParam(value = "pType") String pType) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			resultMap.put("menuIds", authService.load(pobjId, pobjType, pType));
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}

	/**
	 * 保存授权数据
	 * 
	 * @param pobjId
	 * @param pobjType
	 * @param pType
	 * @param menuIds
	 * @return
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public Map<String, Object> save(@RequestParam(value = "pobjId") Long pobjId,
			@RequestParam(value = "pobjType") String pobjType, @RequestParam(value = "pType") String pType,
			@RequestParam(value = "menuIds") String menuIds) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			authService.save(pobjId, pobjType, pType, menuIds);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}
}
